var url = require("url");
var fs = require("fs");

var hotServices = require("../dservices/hotservices.js");

exports.routeRequest = function (request, response)
{
    var pathname = url.parse(request.url).pathname;
    console.log("[SERVER]-> Received request for " + pathname);
      
    if(pathname == "/getHotVideos")
    {
        hotServices.getVideos(response, serviceCallback);
    }
    else if(pathname == "/")
    {
        var fileToReturn = "./public/index.html";
        
        fs.readFile(fileToReturn, function (err, data){
            response.writeHead(200, {'Content-Type': 'text/html','Content-Length': data.length});
            response.write(data);
            response.end();
        });
    }
    else
    {
        var fileToReturn = "./public" + pathname;
        
        fs.readFile(fileToReturn, function (err, data){
            response.write(data);
            response.end();
        });
    }
};

function serviceCallback(response, result)
{
    response.writeHead("200", {"Content-Type" : "jsonp", "Access-Control-Allow-Origin" : "*"});
    response.write(JSON.stringify(result));
    response.end();
}
